package com.uinnova.springboot.demo.asynchronization;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @author 张攀钦
 * @date 2018/12/21-17:02
 * 异步测试
 *  @Async 所修饰的函数不要定义为static类型
 */
@Service
public class MsgService {
    @Async(value = "taskExecutor")
    public void sendA() throws Exception {
        System.out.println("send A");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime));
    }

    @Async(value = "taskExecutor")
    public void sendB() throws Exception {
        System.out.println("send B");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime));
    }

    @Async(value = "taskExecutor")
    public Future<String> sendC() throws Exception {
        System.out.println("send C");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime));
        return new AsyncResult<String>("sendC");
    }

    @Async(value = "taskExecutor")
    public Future<String> sendD() throws Exception {
        System.out.println("send D");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime));
        return new AsyncResult<String>("sendD");
    }
}